import { defineStore } from 'pinia'
import { cloneDeep } from 'lodash'
import {getRole} from '@u/user'
interface Menu {
  label: string,
  icon?: string,
  path?: string,
  roles: string | string[],
  children?: Menu[]
}

interface UserState {
  menus: Menu[]
}
const filterMenus = (menus: Menu[]):Menu[] => {
  const _menus = cloneDeep(menus);
  const authMenus: any[] = [];
  const role = getRole();
  _menus.forEach((menu: Menu) => {
    if (menu.roles === '*' || menu.roles.includes(role as string)) {
      // 递归调用处理 children
      if (menu.children) {
        menu.children = filterMenus(menu.children)
      }

      authMenus.push(menu)
    }
  })

  return authMenus;
}

const useAuthStore = defineStore('auth', {
  state: ():UserState  => ({
    menus: [
      {
        label: "仪表盘",
        icon: 'Odometer',
        path: '/dashBoard',
        roles: "*"
      },
      {
        label: "商品",
        icon: 'Goods',
        roles: "*",
        children: [
          {
            label: "商品管理",
            path: '/itemLists',
            roles: [ 'a', 'b']
          },
          {
            label: "商品分类管理",
            path: '/cateLists',
            roles: ['admin', 'a', 'b']
          }
        ]
      },
      {
        label: "个人中心",
        icon: 'User',
        path: '/userInfo',
        roles: ['admin', 'a', 'b']
      },
      {
        label: "订单管理",
        icon: 'Present',
        path: '/orderLists',
        roles: ['a', 'b']
      },
    ]
  }),
  getters: {
    authMenus(): Menu[] {
      return filterMenus(this.menus)
    }
  },
  actions: {
   
  }
})

export default useAuthStore
